﻿namespace Cubezicles
{
    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Input;

    public class Clock : DrawableGameComponent
    {
        // Different time speeds...........
        public enum TimeSpeed
        {
            //The 60 is to convert from seconds to minutes
            Paused = 0,
            SpeedPlay = 1 * 4 * 60, // lizz likes 4 (5 pushing a little) as the multiplier... was at 10
            SpeedFF = 4 * 4 * 60,
            SpeedFFF = 16 * 4 * 60
        }

        private const string timeFormat = "Day {0:d: hh:mm tt}";

        // Get the Game1 object stored by the base class
        private Game1 game1 { get { return (Game1)Game; } }

        public TimeSpeed CurrTimeSpeed = TimeSpeed.SpeedPlay;
        public TimeSpeed oldSpeed = TimeSpeed.Paused;

        // The current time
        //public long time { get; set; }
        public int days { get; set; }
        public TimeSpan time;

        int startWork = 8;
        int goHomeTime = 17;
        int endWork = 18;

        public Clock(Game1 game): base(game)
        {
            this.DrawOrder = (int)Game1.DisplayOrder.UI;
            // Give the player a starting lump sum of money
            time = new TimeSpan(startWork, 1, 0);
            days = 1;
        }

        public void PauseUnpause()
        {
                TimeSpeed o = CurrTimeSpeed;
                CurrTimeSpeed = oldSpeed;
                oldSpeed = o;
        }

        public override void Update(GameTime gameTime)
        {
            //-- Keyboard user controls for game speed --//
            KeyboardState keys = Keyboard.GetState();

            // 1 = normal speed
            if (keys.IsKeyDown(Keys.D1)) CurrTimeSpeed = TimeSpeed.SpeedPlay;
            // 2 = fast forward (speed x2)
            else if (keys.IsKeyDown(Keys.D2)) CurrTimeSpeed = TimeSpeed.SpeedFF;
            // 3 = super fast forward (speed x3)
            else if (keys.IsKeyDown(Keys.D3)) CurrTimeSpeed = TimeSpeed.SpeedFFF;
            // P = pause or unpause
            else if ((keys.IsKeyDown(Keys.P) && game1.oldks.IsKeyUp(Keys.P))
                || (keys.IsKeyDown(Keys.OemTilde) && game1.oldks.IsKeyUp(Keys.OemTilde))
                || (keys.IsKeyDown(Keys.Space) && game1.oldks.IsKeyUp(Keys.Space)))
            {
                PauseUnpause();
            }
            // Update game time
            time += new TimeSpan(gameTime.ElapsedGameTime.Ticks * (int)CurrTimeSpeed); //timeSpeed;

            //Tell all the employees to go home and give them an hour to walk to the door
            if (time.Hours > goHomeTime)
            {
                game1.EmployeeManager.EmpGoHomeAlready();
            }

            // End of the Day Stuff
            if (time.Hours >= endWork)
            {
                if (CurrTimeSpeed != TimeSpeed.Paused)
                    game1.MoneyManager.reported = false;
                time = new TimeSpan(startWork, 0, 0);
                days++;
                oldSpeed = CurrTimeSpeed;
                CurrTimeSpeed = TimeSpeed.Paused;
            }
            base.Update(gameTime);
        }

        // todo:  move calling end of day display/update to Game1... should be handled there
        public override void Draw(GameTime gameTime)
        {
            if (ReachedEOD())
            {
                game1.MoneyManager.DisplayEODReport();
                game1.MoneyManager.reported = true;
                game1.MoneyManager.dailyExpenses = 0;
            }

            base.Draw(gameTime);
        }
       
        public string display()
        {
            var dt = new DateTime().Add(time).AddDays(days - 1);
            return string.Format(timeFormat, dt);
        }

        /// <summary>
        /// Determine if we reached the end of the work day.
        /// </summary>
        /// <returns></returns>
        public bool ReachedEOD()
        {
            return time.Hours == startWork && time.Minutes == 0;
        }

        // todo: *** the game was reset
        public void Reset()
        {
            // todo: reset clock
        }
    }
}
